package HTTP

import (
	"SQL/util"
	"encoding/json"
	"logs"
	"net/http"
)

// s objectID  string,nickname string,icon string,customInfo string
// r Message string,
func alterObjectData(w http.ResponseWriter, r *http.Request) {

	code := Success
	info := "success"
	var information interface{}

	defer func() {
		writeResult(w, code, info, information)
	}()

	config(w, r)

	err := r.ParseForm()
	if err != nil {
		code = errParseForm
		logs.Print("From:alterObjectData.ParseForm:---", err.Error())
		info = "From:alterObjectData.ParseForm:---" + err.Error()
		return
	}

	id := r.Form["objectID"]
	nickName := r.Form["nickname"]
	icon := r.Form["icon"]
	customList := r.Form["customInfo"]

	if id == nil {
		code = errEmtpyID
		logs.Print("From:alterObjectData.ParseForm:---objectID is null")
		info = "From:alterObjectData:---objectID is null"
		return
	}

	if nickName == nil {
		code = errInvalidNickname
		logs.Print("From:alterObjectData:---nickName is null")
		info = "From:alterObjectData:---nickName is null"
		return
	}
	if icon == nil {
		code = errInvalidIcon
		logs.Print("From:alterObjectData:---icon is null")
		info = "From:alterObjectData:---icon is null"
		return
	}

	if customList == nil {
		code = errInvalidCustomInfo
		logs.Print("From:alterObjectData:---customInfo is null")
		info = "From:alterObjectData:---customInfo is null"
		return
	}

	info, code, ok := checkOutAlterObjectData(id[0], nickName[0], icon[0], customList[0])
	if !ok {
		return
	}

	if icon[0] == "" || icon[0] == "\"\"" {
		icon[0] = "icon1"
	}

	if nickName[0] == "" {
		nickName[0] = " "
	}

	data := []byte(customList[0])
	var customMap map[string]interface{}
	err = json.Unmarshal(data, &customMap)
	if err != nil {
		code = errMarshalJSONFailed
		logs.Print("From:alterObjectData.Unmarshal:---", err.Error())
		info = "From:alterObjectData.Unmarshal:---json.Unmarshal error:---" + err.Error()
	}

	custom, err := json.Marshal(customMap)
	if err != nil {
		code = errMarshalJSONFailed
		logs.Print("From:alterObjectData.Marshal:---", err.Error())
		info = "From:alterObjectData.Marshal:---json.Marshal error"
	}

	info, ok = updateObject(id[0], nickName[0], icon[0], custom)
	if !ok {
		code = errDBExecFailed
		return
	}

}

func updateObject(id string, nickName string, icon string, custom []byte) (string, bool) {

	info := "success"
	if id == "" {
		logs.Print("From:alterObjectData:--- id输入有误,可能路径有误")
		info = "objectid  is null"
		return info, false
	}

	if icon == "" || icon == "\"\"" {
		icon = "default icon"
	}

	if nickName == "" {
		nickName = " "
	}

	sql := "update objects set nickname = $1, icon = $2,custom = $3 where id = $4"
	_, err := util.Exec(sql, nickName, icon, custom, id)
	if err != nil {
		logs.Print("From:alterObjectData.Exec:---", err.Error())
		info = "From:alterObjectData.Exec:--- error"
		return info, false
	}
	return info, true
}

// func convertMap(jsonInfo string) (map[string]interface{}, bool) {

// 	result := make(map[string]interface{})

// 	//jsonInfo = "{sex:male;age:17;friend:[asd,asd,asd]}"
// 	//strArray = [sex:male	age:17	friend:[asd,asd,asd]]
// 	strArray := strings.Split(strings.Trim(jsonInfo, "{} "), "*")

// 	for i := 0; i < len(strArray); i++ {
// 		valueAarry := strings.Split(strArray[i], ":")
// 		len := len(valueAarry)
// 		if len > 2 {
// 			return nil, false
// 		} else if len == 0 {
// 			return result, true
// 		} else if len == 1 {
// 			result[valueAarry[0]] = ""
// 			continue
// 		}
// 		result[valueAarry[0]] = valueAarry[1]
// 	}

// 	return result, true
// }

func checkOutAlterObjectData(objectID string, nickName string, icon string, customInfo string) (string, int, bool) {
	info := "success"
	code := Success
	ok := true

	if ok, code, info = checkOutId(objectID); !ok {
		info = "From:alterObjectData.checkOutAlterObjectData:---objectID is invlid"
		return info, code, ok
	}

	if ok, code, info = checkOutNickName(nickName); !ok {
		info = "From:alterObjectData.checkOutAlterObjectData:---nickname is invlid"
		return info, code, ok
	}

	if ok, code, info = checkOutJsonInfo(customInfo); !ok {
		info = "From:alterObjectData.checkOutAlterObjectData:---customInfo is invlid"
		return info, code, ok
	}
	info, code, ok = isObject(objectID)
	if !ok {
		info = "From:alterObjectData.checkOutAlterObjectData:---objectID is invlid because objectID is not exist"
		return info, code, ok
	}

	return info, code, ok
}
